package com.bfxy.springcloud.api;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.bfxy.springcloud.entity.User;

@RestController
public class ConsumerController {

	@Autowired
	RestTemplate restTemplate;
	
	@RequestMapping(value = "/get")
	public String get() {
		System.err.println("consumer --> get  ");
	ResponseEntity<User> res= restTemplate.
			getForEntity("http://provider-service/provider/getUser?id=001", User.class);
		User user = res.getBody();
		System.err.println("consumer --> get  "+user.getName());
		return "get ok";
	}
	
	@RequestMapping(value = "/post")
	public String post() {
		System.err.println("consumer --> post  ");
	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	map.set("id", "002");
	ResponseEntity<User> res= restTemplate
			.postForEntity("http://provider-service/provider/postUser",map, User.class);
		User user = res.getBody();
		System.err.println("consumer --> post  "+user.getName());
		return "post ok";
	}
}
